package uranium.gui;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import uranium.db.DBManager;
import uranium.db.model.TableModelAssistenza;
import uranium.db.tabelle.autocomplete.AutoCompleteClienti;
import uranium.db.tabelle.autocomplete.UpperDocument;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.Vector;

import javax.swing.JButton;
import org.jdesktop.swingx.JXTable;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperPrintManager;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.view.JasperViewer;

import java.awt.CardLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;

import com.toedter.calendar.JDateChooser;
import com.toedter.components.JSpinField;
import javax.swing.JComboBox;
import javax.swing.WindowConstants;

public class GestAssExtGUI extends JDialog{

	private JFrame jFrame = null;  //  @jve:decl-index=0:visual-constraint="50,30"
	private JPanel jContentPane = null;
	private JXTable vista = null;
	private DBManager dbm = null;
	private JPanel jPanelCliente = null;
	private JPanel jPanelDati = null;
	private JTextField jTextFieldEmail = null;
	private JTextField jTextFieldCF = null;
	private JTextField jTextFieldCognome = null;
	private JTextField jTextFieldNome = null;
	private JTextField jTextFieldIndirizzo = null;
	private JTextField jTextFieldCap = null;
	private JTextField jTextFieldCitta = null;
	private JTextField jTextFieldFax = null;
	private JTextField jTextFieldTel = null;
	private JTextField jTextFieldNote = null;
	private JTextField jTextFieldImei = null;
	private JTextField jTextFieldModello = null;
	private JTextField jTextFieldDifetto = null;
	private JLabel jLabelCF = null;
	private JLabel jLabelCognome = null;
	private JLabel jLabelNome = null;
	private JLabel jLabelIndirizzo = null;
	private JLabel jLabelCap = null;
	private JLabel jLabelDatScon = null;
	private JLabel jLabelCitta = null;
	private JLabel jLabelNumScon = null;
	private JLabel jLabelFax = null;
	private JLabel jLabelTel = null;
	private JLabel jLabelDataCons = null;
	private JLabel jLabelRip= null;
	private JLabel jLabelEmail = null;
	private JLabel jLabelNote = null;
	private JLabel jLabel10 = null;
	private JLabel jLabelImei = null;
	private JLabel jLabelModello = null;
	private JLabel jLabelDifetto = null;
	private JButton jButtonNuovo = null;
	private JButton jButtonCancella = null;
	private JButton jButtonInserisci = null;
	private JButton jButtonInserisciAss = null;
	private JButton jButtonAggiorna = null;
	private JButton jButtonReset = null;
	private JButton jButtonCancellaAss = null;
	private JButton jButtonCercaClie = null;
	private JLabel jLabelInfoCercaClie = null;
	private JLabel jLabelAccessori = null;
	private JTextField jTextFieldAccessori = null;
	private JLabel jLabelInfoRicerca = null;
	private JButton jButtonCercaAss = null;
	private int idassext = -1;
	private int idcliente = -1;
	private ResultSet rs = null;  //  @jve:decl-index=0:
	private JButton jButtonSuccCliente = null;
	private JButton jButtonCliePrecedente = null;
	private ResultSet rs1 = null;  //  @jve:decl-index=0:
	private JButton jButtonAssSuccessiva = null;
	private JButton jButtonClose = null;
	private JButton jButtonStampa = null;
	private JPanel jPanelBottoni = null;
	private JPanel jPanelVista = null;
	private TableModelAssistenza model = null;
	private JPanel jPanelCont = null;
	private SimpleDateFormat sdf = null;
	
	
	public GestAssExtGUI(JFrame padre, DBManager dbm){
		super(padre, false);
		this.jFrame = padre;
		//jFrame.setEnabled(false);
		this.dbm = dbm;
		initialize();
	}
	
	private void initialize(){
		this.setSize(new Dimension(750, 700));
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		this.setTitle("Gestione Assistenze Esterne");
		this.setContentPane(getJContentPane());
		centro();
		this.setVisible(true);
		sdf = new SimpleDateFormat("yyyy-MM-dd");
	}
	
	private void centro(){
		int x = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() - this.getWidth()) / 2;
		int y = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight() - this.getHeight()) / 2;
		this.setLocation(x, y);
	}
	
	/*public void windowClosed(WindowEvent arg0){
		try{
			this.jFrame.setEnabled(true);
		}
		catch(Exception ea){
			ea.printStackTrace();
		}
	}*/
	
	class MyMouseAdapter extends MouseAdapter{

		public void mouseClicked(MouseEvent e){

			System.out.println("Numero click : " + e.getClickCount());
			if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 2 && e.getSource() == vista){
				// Selezioniamo la riga e prendiamo il
				// suo ID che rappresenta la riga nel DB
				JTable table = (JTable) e.getSource();
				int row = table.getSelectedRow();
				if (row < 0) return;
				int nRiga = ((Integer) table.getValueAt(row, 0)).intValue();
				((CardLayout) jPanelCont.getLayout()).show(jPanelCont, "pnlDati");
				modificaDati(nRiga);
			}
			else if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 1 && e.getSource() == vista){
				// Selezioniamo la riga e prendiamo il
				// suo ID che rappresenta la riga nel DB
				JTable table = (JTable) e.getSource();
				int row = table.getSelectedRow();
				if (row < 0) return;
				idassext = ((Integer) table.getValueAt(row, 0)).intValue();
				idcliente = Integer.parseInt((String)table.getValueAt(row, 1));
				cercaClienteXID();
			}

		}

	}

	/**
	 * This method initializes jContentPane	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJPanelCliente(), BorderLayout.NORTH);
			//jContentPane.add(getJPanelDati(), BorderLayout.CENTER);
			jContentPane.add(getJPanelBottoni(), BorderLayout.SOUTH);
			//jContentPane.add(getJPanelVista(), BorderLayout.CENTER);
			jContentPane.add(getJPanelCont(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelCliente() {
		if (jPanelCliente == null) {
			jLabelInfoCercaClie = new JLabel();
			jLabelInfoCercaClie.setBounds(new Rectangle(53, 157, 300, 16));
			jLabelInfoCercaClie.setText("Per la ricerca bisogna inserire Cognome e Nome.");
			jPanelCliente = new JPanel();		
			jLabel10 = new JLabel();
			jLabel10.setBounds(new Rectangle(53, 135, 335, 16));
			jLabel10.setText("* Campi Obbligatori per l'inserimento di un nuovo Cliente.");
			jLabelNote = new JLabel();
			jLabelNote.setText("Note");
			jLabelNote.setBounds(new Rectangle(378, 113, 36, 16));
			jLabelEmail = new JLabel();
			jLabelEmail.setText("e-mail");
			jLabelEmail.setBounds(new Rectangle(378, 91, 45, 16));
			jLabelFax = new JLabel();
			jLabelFax.setText("Fax");
			jLabelFax.setBounds(new Rectangle(53, 113, 30, 16));
			jLabelTel = new JLabel();
			jLabelTel.setText("Telefono");
			jLabelTel.setBounds(new Rectangle(53, 91, 59, 16));
			jLabelCitta = new JLabel();
			jLabelCitta.setText("Citta'");
			jLabelCitta.setBounds(new Rectangle(378, 69, 39, 16));
			jLabelCap = new JLabel();
			jLabelCap.setText("CAP");
			jLabelCap.setBounds(new Rectangle(53, 69, 34, 16));
			jLabelIndirizzo = new JLabel();
			jLabelIndirizzo.setText("Indirizzo");
			jLabelIndirizzo.setBounds(new Rectangle(378, 47, 57, 16));
			jLabelNome = new JLabel();
			jLabelNome.setText("Nome *");
			jLabelNome.setBounds(new Rectangle(53, 47, 43, 16));
			jLabelCognome = new JLabel();
			jLabelCognome.setText("Cognome *");
			jLabelCognome.setBounds(new Rectangle(378, 25, 64, 16));
			jLabelCF = new JLabel();
			jLabelCF.setText("CF / P.IVA");
			jLabelCF.setBounds(new Rectangle(53, 25, 95, 16));
			jPanelCliente.setLayout(null);
			jPanelCliente.setPreferredSize(new Dimension(250, 250));
			jPanelCliente.add(getJTextFieldEmail(), null);
			jPanelCliente.add(jLabelCF, null);
			jPanelCliente.add(jLabelCognome, null);
			jPanelCliente.add(getJTextFieldCF(), null);
			jPanelCliente.add(jLabelNome, null);
			jPanelCliente.add(getJTextFieldCognome(), null);
			jPanelCliente.add(jLabelIndirizzo, null);
			jPanelCliente.add(getJTextFieldNome(), null);
			jPanelCliente.add(jLabelCap, null);
			jPanelCliente.add(getJTextFieldIndirizzo(), null);
			jPanelCliente.add(jLabelCitta, null);
			jPanelCliente.add(jLabelTel, null);
			jPanelCliente.add(getJTextFieldCap(), null);
			jPanelCliente.add(getJTextFieldCitta(), null);
			jPanelCliente.add(jLabelFax, null);
			jPanelCliente.add(jLabelEmail, null);
			jPanelCliente.add(jLabelNote, null);
			jPanelCliente.add(getJTextFieldFax(), null);
			jPanelCliente.add(getJTextFieldTel(), null);
			jPanelCliente.add(getJTextFieldNote(), null);
			jPanelCliente.add(jLabel10, null);
			jPanelCliente.add(getJButtonNuovo(), null);
			jPanelCliente.add(getJButtonCancella(), null);
			jPanelCliente.add(getJButtonInserisci(), null);
			jPanelCliente.add(getJButtonCercaClie(), null);
			jPanelCliente.add(jLabelInfoCercaClie, null);
			jPanelCliente.add(getJButtonSuccCliente(), null);
			jPanelCliente.add(getJButtonCliePrecedente(), null);
		}
		return jPanelCliente;
	}
	
	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldEmail() {
		if (jTextFieldEmail == null) {
			jTextFieldEmail = new JTextField();
			jTextFieldEmail.setPreferredSize(new Dimension(200, 22));
			jTextFieldEmail.setBounds(new Rectangle(484, 88, 200, 22));
		}
		return jTextFieldEmail;
	}

	/**
	 * This method initializes jTextField1	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldCF() {
		if (jTextFieldCF == null) {
			jTextFieldCF = new JTextField();
			jTextFieldCF.setPreferredSize(new Dimension(200, 22));
			jTextFieldCF.setBounds(new Rectangle(161, 22, 200, 22));
		}
		return jTextFieldCF;
	}

	/**
	 * This method initializes jTextField2	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldCognome() {
		if (jTextFieldCognome == null) {
			jTextFieldCognome = new JTextField();
			AutoCompleteClienti complete = new AutoCompleteClienti(jTextFieldCognome, dbm, "cognome");
			dbm.addDBStateChange(complete);
			jTextFieldCognome.setPreferredSize(new Dimension(200, 22));
			jTextFieldCognome.setBounds(new Rectangle(484, 22, 200, 22));
			jTextFieldCognome.setDocument(new UpperDocument(complete, false));
		}
		return jTextFieldCognome;
	}

	/**
	 * This method initializes jTextField3	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldNome() {
		if (jTextFieldNome == null) {
			jTextFieldNome = new JTextField();
			AutoCompleteClienti complete = new AutoCompleteClienti(jTextFieldNome, dbm, "nome");
			dbm.addDBStateChange(complete);
			jTextFieldNome.setPreferredSize(new Dimension(200, 22));
			jTextFieldNome.setBounds(new Rectangle(161, 44, 200, 22));
			jTextFieldNome.setDocument(new UpperDocument(complete, false));
		}
		return jTextFieldNome;
	}

	/**
	 * This method initializes jTextField4	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldIndirizzo() {
		if (jTextFieldIndirizzo == null) {
			jTextFieldIndirizzo = new JTextField();
			jTextFieldIndirizzo.setPreferredSize(new Dimension(200, 22));
			jTextFieldIndirizzo.setBounds(new Rectangle(484, 44, 200, 22));
		}
		return jTextFieldIndirizzo;
	}
	
	/**
	 * This method initializes jTextField5	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldCap() {
		if (jTextFieldCap == null) {
			jTextFieldCap = new JTextField();
			jTextFieldCap.setPreferredSize(new Dimension(200, 22));
			jTextFieldCap.setBounds(new Rectangle(161, 66, 200, 22));
		}
		return jTextFieldCap;
	}

	/**
	 * This method initializes jTextField6	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldCitta() {
		if (jTextFieldCitta == null) {
			jTextFieldCitta = new JTextField();
			jTextFieldCitta.setPreferredSize(new Dimension(200, 22));
			jTextFieldCitta.setBounds(new Rectangle(484, 66, 200, 22));
		}
		return jTextFieldCitta;
	}

	/**
	 * This method initializes jTextField7	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldFax() {
		if (jTextFieldFax == null) {
			jTextFieldFax = new JTextField();
			jTextFieldFax.setPreferredSize(new Dimension(200, 22));
			jTextFieldFax.setBounds(new Rectangle(161, 110, 200, 22));
		}
		return jTextFieldFax;
	}

	/**
	 * This method initializes jTextField8	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldTel() {
		if (jTextFieldTel == null) {
			jTextFieldTel = new JTextField();
			jTextFieldTel.setPreferredSize(new Dimension(200, 22));
			jTextFieldTel.setBounds(new Rectangle(161, 88, 200, 22));
		}
		return jTextFieldTel;
	}

	/**
	 * This method initializes jTextField9	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldNote() {
		if (jTextFieldNote == null) {
			jTextFieldNote = new JTextField();
			jTextFieldNote.setPreferredSize(new Dimension(200, 22));
			jTextFieldNote.setBounds(new Rectangle(484, 110, 200, 22));
		}
		return jTextFieldNote;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelDati() {
		if (jPanelDati == null) {
			jLabelRicambi = new JLabel();
			jLabelRicambi.setBounds(new Rectangle(340, 136, 60, 16));
			jLabelRicambi.setText("Ricambi");
			jLabelInterventi = new JLabel();
			jLabelInterventi.setBounds(new Rectangle(15, 158, 115, 16));
			jLabelInterventi.setText("Intervento Eseguito");
			jLabelCortesia = new JLabel();
			jLabelCortesia.setBounds(new Rectangle(15, 136, 90, 16));
			jLabelCortesia.setText("App. Cortesia");
			jLabelCampiObbl = new JLabel();
			jLabelCampiObbl.setBounds(new Rectangle(15, 175, 320, 16));
			jLabelCampiObbl.setText("* Campi Obbligatori.");
			jPanelDati = new JPanel();
			jPanelDati.setLayout(null);
			jPanelDati.setName("pnlDati");
			jPanelDati.setPreferredSize(new Dimension(250, 100));
			jPanelDati.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory
					.createBevelBorder(javax.swing.border.BevelBorder.RAISED), "Dati Assistenza",
					javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
					javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null)); // Generated*/
			
			
			jLabelAccessori = new JLabel();
			jLabelAccessori.setBounds(new Rectangle(15, 114, 80, 16));
			jLabelAccessori.setText("Accessori");
			jLabelRip = new JLabel();
			jLabelRip.setBounds(new Rectangle(340, 92, 80, 16));
			jLabelRip.setText("Riparato");
			jLabelDataCons = new JLabel();
			jLabelDataCons.setBounds(new Rectangle(15, 92, 90, 16));
			jLabelDataCons.setText("Data Consegna");
			jLabelNumScon = new JLabel();
			jLabelNumScon.setText("Numero Scontrino *");
			jLabelNumScon.setBounds(new Rectangle(340, 70, 117, 16));
			jLabelDatScon = new JLabel();
			jLabelDatScon.setText("Data Scontrino *");
			jLabelDatScon.setBounds(new Rectangle(15, 70, 98, 16));
			jLabelDifetto = new JLabel();
			jLabelDifetto.setText("Difetto *");
			jLabelDifetto.setBounds(new Rectangle(15, 48, 63, 16));
			jLabelModello = new JLabel();
			jLabelModello.setText("Modello *");
			jLabelModello.setBounds(new Rectangle(340, 26, 54, 16));
			jLabelImei = new JLabel();
			jLabelImei.setText("IMEI *");
			jLabelImei.setBounds(new Rectangle(15, 26, 36, 16));
			
			jPanelDati.add(getJTextFieldImei(), null);
			jPanelDati.add(jLabelImei, null);
			jPanelDati.add(getJTextFieldModello(), null);
			jPanelDati.add(jLabelModello, null);
			jPanelDati.add(getJTextFieldDifetto(), null);
			jPanelDati.add(jLabelDifetto, null);
			jPanelDati.add(jLabelDatScon, null);
			jPanelDati.add(jLabelNumScon, null);
			jPanelDati.add(jLabelDataCons, null);
			jPanelDati.add(jLabelRip, null);
			jPanelDati.add(jLabelAccessori, null);
			jPanelDati.add(getJTextFieldAccessori(), null);
			jPanelDati.add(getJButtonAssSuccessiva(), null);
			jPanelDati.add(jLabelCampiObbl, null);
			jPanelDati.add(getJDateChooserDataScon(), null);
			jPanelDati.add(getJSpinFieldNumScon(), null);
			jPanelDati.add(getJDateChooserDataCon(), null);
			jPanelDati.add(getJComboBoxRiparato(), null);		
			jPanelDati.add(getJButtonReset(), null);
			jPanelDati.add(jLabelCortesia, null);
			jPanelDati.add(getJTextFieldCortesia(), null);
			jPanelDati.add(jLabelInterventi, null);
			jPanelDati.add(getJTextFieldIntervento(), null);
			jPanelDati.add(jLabelRicambi, null);
			jPanelDati.add(getJTextFieldRicambi(), null);
		}
		return jPanelDati;
	}
	
	private JTextField getJTextFieldImei() {
		if (jTextFieldImei == null) {
			jTextFieldImei = new JTextField();
			jTextFieldImei.setPreferredSize(new Dimension(200, 22));
			jTextFieldImei.setBounds(new Rectangle(123, 23, 200, 22));
		}
		return jTextFieldImei;
	}

	/**
	 * This method initializes jTextField1	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldModello() {
		if (jTextFieldModello == null) {
			jTextFieldModello = new JTextField();
			jTextFieldModello.setPreferredSize(new Dimension(200, 22));
			jTextFieldModello.setBounds(new Rectangle(446, 23, 200, 22));
		}
		return jTextFieldModello;
	}

	/**
	 * This method initializes jTextField2	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldDifetto() {
		if (jTextFieldDifetto == null) {
			jTextFieldDifetto = new JTextField();
			jTextFieldDifetto.setPreferredSize(new Dimension(200, 22));
			jTextFieldDifetto.setBounds(new Rectangle(123, 45, 523, 22));
		}
		return jTextFieldDifetto;
	}

	/**
	 * This method initializes jButtonNuovo	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonNuovo() {
		if (jButtonNuovo == null) {
			jButtonNuovo = new JButton();
			jButtonNuovo.setBounds(new Rectangle(488, 204, 90, 30));
			jButtonNuovo.setText("Reset");
			jButtonNuovo.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					resetCliente();
				}
			});
		}
		return jButtonNuovo;
	}

	/**
	 * This method initializes jButtonCancella	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonCancella() {
		if (jButtonCancella == null) {
			jButtonCancella = new JButton();
			jButtonCancella.setBounds(new Rectangle(593, 159, 90, 30));
			jButtonCancella.setText("Cancella");
			jButtonCancella.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					int scelta = JOptionPane.showConfirmDialog(jFrame, "Sei sicuro?", "Conferma eliminazione", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
					if (scelta==0){
						int i = deleteCliente();
						if( i == 0)
							JOptionPane.showMessageDialog(jFrame, "Rimozione non riuscita.", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
						else
							JOptionPane.showMessageDialog(jFrame, "Rimozione effettuata.", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
					}
				}
			});
		}
		return jButtonCancella;
	}

	/**
	 * This method initializes jButtonInserisci	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonInserisci() {
		if (jButtonInserisci == null) {
			jButtonInserisci = new JButton();
			jButtonInserisci.setBounds(new Rectangle(593, 204, 90, 30));
			jButtonInserisci.setText("Inserisci");
			jButtonInserisci.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					try{
						int i = inserisciCliente();
						if( i== 0)
							JOptionPane.showMessageDialog(jFrame, "Inserimento non riuscito.", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
						else
							JOptionPane.showMessageDialog(jFrame, "Inserimento effettuato.", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
					}
					catch(Exception ee){
						System.err.println(ee);
					}
				}
			});
		}
		return jButtonInserisci;
	}

	/**
	 * This method initializes jButtonInserisciAss	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonInserisciAss() {
		if (jButtonInserisciAss == null) {
			jButtonInserisciAss = new JButton();
			jButtonInserisciAss.setBounds(new Rectangle(593, 20, 90, 30));
			jButtonInserisciAss.setText("Inserisci");
			jButtonInserisciAss.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					int i = inserisciAssExt();
					if( i== 0)
						JOptionPane.showMessageDialog(jFrame, "Inserimento non riuscito.", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
					else{
						JOptionPane.showMessageDialog(jFrame, "Inserimento effettuato.", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
						cercaAssXClie();
					}
				}
			});
		}
		return jButtonInserisciAss;
	}

	/**
	 * This method initializes jButtonAggiorna	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonAggiorna() {
		if (jButtonAggiorna == null) {
			jButtonAggiorna = new JButton();
			jButtonAggiorna.setBounds(new Rectangle(488, 20, 90, 30));
			jButtonAggiorna.setText("Salva");
			jButtonAggiorna.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					int i = updateAssExt();
					if( i== 0)
						JOptionPane.showMessageDialog(jFrame, "Aggiornamento  non riuscito.", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
					else{
						JOptionPane.showMessageDialog(jFrame, "Aggiornamento effettuato.", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
						cercaAssXClie();
					}
				}
			});
		}
		return jButtonAggiorna;
	}

	/**
	 * This method initializes jButtonReset	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonReset() {
		if (jButtonReset == null) {
			jButtonReset = new JButton();
			jButtonReset.setText("Reset");
			jButtonReset.setBounds(new Rectangle(555, 235, 90, 30));
			jButtonReset.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					resetAss();
				}
			});
		}
		return jButtonReset;
	}

	/**
	 * This method initializes jButtonCancellaAss	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonCancellaAss() {
		if (jButtonCancellaAss == null) {
			jButtonCancellaAss = new JButton();
			jButtonCancellaAss.setBounds(new Rectangle(488, 65, 90, 30));
			jButtonCancellaAss.setText("Cancella");
			jButtonCancellaAss.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					int scelta = JOptionPane.showConfirmDialog(jFrame, "Sei sicuro?", "Conferma eliminazione", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
					if (scelta==0){
						int i = deleteAssExt();
						if( i== 0)
							JOptionPane.showMessageDialog(jFrame, "Rimozione non effettuata.", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
						else{
							JOptionPane.showMessageDialog(jFrame, "Rimozione effettuata.", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
							cercaAssXClie();
						}
					}
				}
			});
		}
		return jButtonCancellaAss;
	}

	/**
	 * This method initializes jButtonCercaClie	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonCercaClie() {
		if (jButtonCercaClie == null) {
			jButtonCercaClie = new JButton();
			jButtonCercaClie.setBounds(new Rectangle(488, 159, 90, 30));
			jButtonCercaClie.setText("Cerca");
			jButtonCercaClie.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					cercaCliente();
				}
			});
		}
		return jButtonCercaClie;
	}

	/**
	 * This method initializes jTextFieldAccessori	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldAccessori() {
		if (jTextFieldAccessori == null) {
			jTextFieldAccessori = new JTextField();
			jTextFieldAccessori.setBounds(new Rectangle(123, 111, 523, 22));
		}
		return jTextFieldAccessori;
	}

	/**
	 * This method initializes jButtonCercaAss	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonCercaAss() {
		if (jButtonCercaAss == null) {
			jButtonCercaAss = new JButton();
			jButtonCercaAss.setBounds(new Rectangle(165, 41, 90, 30));
			jButtonCercaAss.setText("Cerca");
			jButtonCercaAss.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					cercaAssXID();
				}
			});
		}
		return jButtonCercaAss;
	}
	
	private void resetCliente(){
		try{
			idcliente = -1;
			jTextFieldEmail.setText("");
			jTextFieldCF.setText("");
			jTextFieldCognome.setText("");
			jTextFieldNome.setText("");
			jTextFieldIndirizzo.setText("");
			jTextFieldCap.setText("");
			jTextFieldCitta.setText("");
			jTextFieldFax.setText("");
			jTextFieldTel.setText("");
			jTextFieldNote.setText("");
			rs = null;
			model = new TableModelAssistenza(dbm, "", "", "assext", "");
			vista .setModel(model);
			
		}
		catch(Exception er){
			er.printStackTrace();
		}
	}
	
	private void resetAss(){
		try{
			idassext = -1;
			jTextFieldImei.setText("");
			jTextFieldModello.setText("");
			jTextFieldDifetto.setText("");
			jDateChooserDataScon.setDate(null);
			jSpinFieldNumScon.setValue(0);
			jDateChooserDataCon.setDate(null);
			jComboBoxRiparato.setSelectedItem("");
			jTextFieldAccessori.setText("");
			jTextFieldCortesia.setText("");
			jTextFieldIntervento.setText("");
			jTextFieldRicambi.setText("");
			rs1 = null;
		}
		catch(Exception er) {
			er.printStackTrace();
		}
	}
	
	private synchronized int inserisciCliente() throws SQLException{
		boolean error = false;
		if(jTextFieldCognome.getText().trim().equals("")){
			JOptionPane.showMessageDialog(jFrame, "Campo Cognome non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		if(jTextFieldNome.getText().trim().equals("")){
			JOptionPane.showMessageDialog(jFrame, "Campo Nome non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		
		if(error == false){
			int id = getNewIDCliente();
			String cf = jTextFieldCF.getText();
			String cognome = jTextFieldCognome.getText();
			String nome = jTextFieldNome.getText();
			String ind = jTextFieldIndirizzo.getText();
			String cap = jTextFieldCap.getText();
			String citta = jTextFieldCitta.getText();
			String tel = jTextFieldTel.getText();
			String fax = jTextFieldFax.getText();
			String email = jTextFieldEmail.getText();
			String note = jTextFieldNote.getText();
			
			String insert="INSERT INTO clienti (id,cf,cognome,nome,indirizzo,cap,citta,tel,fax,email,note) VALUES('"+id+"','"+cf+"','"+cognome+"','"+nome+"','"+ind+"','"+cap+"','"+citta+"','"+tel+"','"+fax+"','"+email+"','"+note+"')";
			dbm.executeUpdate(insert);
			idcliente = id;
			return 1;
		}
		return 0;
	}
	
	private synchronized int inserisciAssExt(){
		boolean error = false;
		if(jTextFieldImei.getText().trim().equals("")){
			JOptionPane.showMessageDialog(jFrame, "Campo IMEI non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		if(jTextFieldModello.getText().trim().equals("")){
			JOptionPane.showMessageDialog(jFrame, "Campo Modello non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		if(jTextFieldDifetto.getText().trim().equals("")){
			JOptionPane.showMessageDialog(jFrame, "Campo Difetto non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		if(jDateChooserDataScon == null){
			JOptionPane.showMessageDialog(jFrame, "Campo Data Scontrino non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		if(jSpinFieldNumScon.getValue() == 0){
			JOptionPane.showMessageDialog(jFrame, "Campo Numero Scontrino non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		/*if(jTextFieldDataRic.getText().trim().equals("")){
			JOptionPane.showMessageDialog(jFrame, "Campo Data ricezione non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}*/
		if(idcliente == -1){
			JOptionPane.showMessageDialog(jFrame, "Campo Cliente non valido", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			error = true;
		}
		
		if(error == false){
			int id = getNewID();
			String imei = jTextFieldImei.getText();
			String modello = jTextFieldModello.getText();
			String difetto = jTextFieldDifetto.getText();
			String datacon = "";
			if(jDateChooserDataCon.getDate() != null){
				System.out.println(jDateChooserDataCon.getDate());
				SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
				datacon = (String)sdf1.format(jDateChooserDataCon.getDate());
			}
			String datasc = "";
			if(jDateChooserDataScon.getDate() != null){
				//System.out.println(jDateChooserDataCon.getDate());
				SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
				datasc = (String)sdf1.format(jDateChooserDataScon.getDate());
			}
			//String datasc = sdf.format(jDateChooserDataScon.getDate());
			//String datacon = sdf.format(jDateChooserDataCon.getDate());
			String numsc = String.valueOf(jSpinFieldNumScon.getValue());
			String riparato = (String)jComboBoxRiparato.getSelectedItem();
			String cliente = String.valueOf(idcliente);
			String accessori = jTextFieldAccessori.getText();
			String cortesia = jTextFieldCortesia.getText();
			String intervento = jTextFieldIntervento.getText();
			String ricambi = jTextFieldRicambi.getText();
			Date d = new Date();
			Calendar c = new GregorianCalendar();
			c.setTime(d);
			int g = -1, m = -1, a = -1;
			g = c.get(Calendar.DAY_OF_MONTH);
			m = (c.get(Calendar.MONTH)) + 1;
			a = c.get(Calendar.YEAR);
			String dataric = String.valueOf(a)+"-"+String.valueOf(m)+"-"+String.valueOf(g);
			String insert="INSERT INTO assext (id,cliente,imei,modello,difetto,datasc,numsc,dataric,datacon,riparato,accessori,cortesia,intervento,ricambi) VALUES('"+id+"','"+cliente+"','"+imei+"','"+modello+"','"+difetto+"','"+datasc+"','"+numsc+"','"+dataric+"','"+datacon+"','"+riparato+"','"+accessori+"','"+cortesia+"','"+intervento+"','"+ricambi+"')";

			dbm.executeQuery(insert);
			return 1;
		}
		return 0;
	}
	
	private int getNewID()
	{
		ResultSet rs3 = null;
		int newID = 0;
		String query = "";
		query = "SELECT MAX(id) FROM assext";
		try
		{
			rs3 = dbm.executeQuery(query);
			rs3.next();
			newID = (rs3.getInt(1)) + 1;
		} catch (SQLException e)
		{
			e.printStackTrace();
		}
		return newID;
	}
	
	private int getNewIDCliente()
	{
		ResultSet rs3 = null;
		int newID = 0;
		String query = "";
		query = "SELECT MAX(id) FROM clienti";
		try
		{
			rs3 = dbm.executeQuery(query);
			rs3.next();
			newID = (rs3.getInt(1)) + 1;
		} catch (SQLException e)
		{
			e.printStackTrace();
		}
		return newID;
	}
	
	private synchronized int deleteAssExt(){
		try{
			if(idassext == -1){
				JOptionPane.showMessageDialog(jFrame, "Selezionare pratica da eliminare", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			}
			else{
				String query = "DELETE FROM assext WHERE id="+idassext;
				dbm.executeQuery(query);
				resetAss();
				return 1;
			}
		}
		catch(Exception er){
			er.printStackTrace();
		}
		return 0;
	}
	
	private synchronized int deleteCliente(){
		try{
			if(idcliente == -1){
				JOptionPane.showMessageDialog(jFrame, "Selezionare un cliente", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			}
			else{
				String query = "DELETE FROM clienti WHERE id="+idcliente;
				dbm.executeQuery(query);
				resetCliente();
				idcliente = -1;
				return 1;
			}
		}
		catch(Exception er){
			er.printStackTrace();
		}
		return 0;
	}
	
	private synchronized int updateAssExt(){
		try{
			if(idassext == -1){
				JOptionPane.showMessageDialog(jFrame, "Selezionare un'assistenza", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			}
			else{
				String imei = jTextFieldImei.getText();
				String modello = jTextFieldModello.getText();
				String difetto = jTextFieldDifetto.getText();
				//String dataric = jTextFieldDataRic.getText();
				String datacon = "";
				if(jDateChooserDataCon.getDate() != null){
					System.out.println(jDateChooserDataCon.getDate());
					SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
					datacon = (String)sdf1.format(jDateChooserDataCon.getDate());
				}
				String datasc = "";
				if(jDateChooserDataScon.getDate() != null){
					System.out.println(jDateChooserDataScon.getDate());
					SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
					datasc = (String)sdf1.format(jDateChooserDataScon.getDate());
				}
				//String datasc = sdf.format(jDateChooserDataScon.getDate());
				//String datacon = sdf.format(jDateChooserDataCon.getDate());
				String numsc = String.valueOf(jSpinFieldNumScon.getValue());
				String riparato = (String)jComboBoxRiparato.getSelectedItem();
				String cliente = String.valueOf(idcliente);
				String accessori = jTextFieldAccessori.getText();
				String cortesia = jTextFieldCortesia.getText();
				String intervento = jTextFieldIntervento.getText();
				String ricambi = jTextFieldRicambi.getText();
				String update = "UPDATE assext SET cliente='" + cliente + "',imei='" + imei + "',modello='" + modello + "',difetto='" + difetto + "',datasc='" + datasc + "',numsc='" + numsc + "',datacon='" + datacon + "',riparato='" + riparato + "',accessori='" + accessori + "',cortesia='" + cortesia + "',intervento='" + intervento + "',ricambi='" + ricambi + "' WHERE id=" + idassext;
				dbm.executeUpdate(update);
				return 1;
			}
		}
		catch(Exception er){
			er.printStackTrace();
		}
		return 0;
	}
	
	private void cercaCliente(){
		try{
			boolean error = false;
			if(jTextFieldCognome.getText().trim().equals("")){
				JOptionPane.showMessageDialog(jFrame, "Impostare il campo Cognome per la ricerca", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
				error = true;
			}
			/*if(jTextFieldNome.getText().trim().equals("")){
				JOptionPane.showMessageDialog(jFrame, "Impostare il campo Nome per la ricerca", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
				error = true;
			}*/
			if(!error){
				String ricerca = "SELECT * FROM clienti WHERE cognome LIKE '%" + jTextFieldCognome.getText() + "%'";
				rs = dbm.executeQuery(ricerca);
				if(rs.next()){
					idcliente = rs.getInt("id");
					jTextFieldEmail.setText(rs.getString("email"));
					jTextFieldCF.setText(rs.getString("cf"));
					jTextFieldCognome.setText(rs.getString("cognome"));
					jTextFieldNome.setText(rs.getString("nome"));
					jTextFieldIndirizzo.setText(rs.getString("indirizzo"));
					jTextFieldCap.setText(rs.getString("cap"));
					jTextFieldCitta.setText(rs.getString("citta"));
					jTextFieldFax.setText(rs.getString("fax"));
					jTextFieldTel.setText(rs.getString("tel"));
					jTextFieldNote.setText(rs.getString("note"));
					cercaAssXClie();
				}
				else
					System.out.println("niente");
			}
		}
		catch(Exception er){
			er.printStackTrace();
		}
	}
	
	private void clienteSuccessivo(){
		try{
			if(rs == null){
				inizializzaRS();
			}
			else if(rs.next()){
				idcliente = rs.getInt("id");
				jTextFieldEmail.setText(rs.getString("email"));
				jTextFieldCF.setText(rs.getString("cf"));
				jTextFieldCognome.setText(rs.getString("cognome"));
				jTextFieldNome.setText(rs.getString("nome"));
				jTextFieldIndirizzo.setText(rs.getString("indirizzo"));
				jTextFieldCap.setText(rs.getString("cap"));
				jTextFieldCitta.setText(rs.getString("citta"));
				jTextFieldFax.setText(rs.getString("fax"));
				jTextFieldTel.setText(rs.getString("tel"));
				jTextFieldNote.setText(rs.getString("note"));
				cercaAssXClie();
			}
			else{
				rs.previous();
				JOptionPane.showMessageDialog(jFrame, "Nussun Ciente Presente", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			}
		}
		catch(Exception er){
			er.printStackTrace();
		}
	}
	
	private void clientePrecedente(){
		try{
			if(rs == null){
				inizializzaRS();
			}
			else if(rs.previous()){
				idcliente = rs.getInt("id");
				jTextFieldEmail.setText(rs.getString("email"));
				jTextFieldCF.setText(rs.getString("cf"));
				jTextFieldCognome.setText(rs.getString("cognome"));
				jTextFieldNome.setText(rs.getString("nome"));
				jTextFieldIndirizzo.setText(rs.getString("indirizzo"));
				jTextFieldCap.setText(rs.getString("cap"));
				jTextFieldCitta.setText(rs.getString("citta"));
				jTextFieldFax.setText(rs.getString("fax"));
				jTextFieldTel.setText(rs.getString("tel"));
				jTextFieldNote.setText(rs.getString("note"));
				cercaAssXClie();
			}
			else{
				rs.next();
				JOptionPane.showMessageDialog(jFrame, "Nussun Ciente Presente", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			}
		}
		catch(Exception er){
			er.printStackTrace();
		}
	}
	
	private void inizializzaRS(){
		try{
			String query = "SELECT * FROM clienti";
			rs = dbm.executeQuery(query);
			clienteSuccessivo();
		}
		catch(Exception er){
			er.printStackTrace();
		}
	}

	/**
	 * This method initializes jButtonSuccCliente	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonSuccCliente() {
		if (jButtonSuccCliente == null) {
			jButtonSuccCliente = new JButton();
			jButtonSuccCliente.setBounds(new Rectangle(303, 189, 90, 30));
			jButtonSuccCliente.setText("Next");
			jButtonSuccCliente.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					clienteSuccessivo();
				}
			});
		}
		return jButtonSuccCliente;
	}

	/**
	 * This method initializes jButtonCliePrecedente	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonCliePrecedente() {
		if (jButtonCliePrecedente == null) {
			jButtonCliePrecedente = new JButton();
			jButtonCliePrecedente.setBounds(new Rectangle(133, 189, 90, 30));
			jButtonCliePrecedente.setText("Prev");
			jButtonCliePrecedente.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					clientePrecedente();
				}
			});
		}
		return jButtonCliePrecedente;
	}
	
	private void cercaAssXClie(){
		try{
			((CardLayout) jPanelCont.getLayout()).show(jPanelCont, "pnlVista");
			model = new TableModelAssistenza(dbm,"cliente", String.valueOf(idcliente), "assext", "");
			vista.setModel(model);
		}
		catch(Exception er){
			er.printStackTrace();
		}
	}

	/**
	 * This method initializes jButtonAssSuccessiva	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonAssSuccessiva() {
		if (jButtonAssSuccessiva == null) {
			jButtonAssSuccessiva = new JButton();
			jButtonAssSuccessiva.setText("Indietro");
			jButtonAssSuccessiva.setBounds(new Rectangle(555, 185, 90, 30));
			jButtonAssSuccessiva.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					cercaAssXClie();
				}
			});
		}
		return jButtonAssSuccessiva;
	}
	
	private void cercaAssXID(){
		try{
			((CardLayout) jPanelCont.getLayout()).show(jPanelCont, "jPanelDati");
			String query = "SELECT * FROM assext WHERE id='"+jTextFieldCercaID.getText()+"'";
			rs1 = dbm.executeQuery(query);
			if(rs1.next()){
				idassext = rs1.getInt("id");
				jTextFieldImei.setText(rs1.getString("imei"));
				jTextFieldModello.setText(rs1.getString("modello"));
				jTextFieldDifetto.setText(rs1.getString("difetto"));
				jDateChooserDataScon.setDate(new Date(rs1.getString("datasc")));
				jDateChooserDataCon.setDate(new Date(rs1.getString("datacon")));
				jSpinFieldNumScon.setValue(Integer.parseInt(rs1.getString("numsc")));
				jComboBoxRiparato.setSelectedItem(rs1.getString("riparato"));
				jTextFieldCortesia.setText(rs1.getString("cortesia"));
				jTextFieldIntervento.setText(rs1.getString("intervento"));
				jTextFieldAccessori.setText(rs1.getString("accessori"));
				jTextFieldRicambi.setText(rs1.getString("ricambi"));
				idcliente = rs1.getInt("cliente");
				cercaClienteXID();
			}
		}
		catch(Exception er){
			er.printStackTrace();
		}
	}
	
	private void cercaClienteXID(){
		try{
			String ricerca = "SELECT * FROM clienti WHERE id="+idcliente;
			rs = dbm.executeQuery(ricerca);
			if(rs.next()){
				idcliente = rs.getInt("id");
				jTextFieldEmail.setText(rs.getString("email"));
				jTextFieldCF.setText(rs.getString("cf"));
				jTextFieldCognome.setText(rs.getString("cognome"));
				jTextFieldNome.setText(rs.getString("nome"));
				jTextFieldIndirizzo.setText(rs.getString("indirizzo"));
				jTextFieldCap.setText(rs.getString("cap"));
				jTextFieldCitta.setText(rs.getString("citta"));
				jTextFieldFax.setText(rs.getString("fax"));
				jTextFieldTel.setText(rs.getString("tel"));
				jTextFieldNote.setText(rs.getString("note"));
			}
		}
		catch(Exception er){
			er.printStackTrace();
		}
	}
	
	private void inizializzaRS1(){
		try{
			String query = "SELECT * FROM assext";
			rs1 = dbm.executeQuery(query);
		}
		catch(Exception er){
			er.printStackTrace();
		}
	}

	/**
	 * This method initializes jButtonClose	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonClose() {
		if (jButtonClose == null) {
			jButtonClose = new JButton();
			jButtonClose.setBounds(new Rectangle(593, 65, 90, 30));
			jButtonClose.setText("Chiudi");
			jButtonClose.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					close();
				}
			});
		}
		return jButtonClose;
	}
	
	private void close(){
		try{
			//jFrame.setEnabled(true);
			this.dispose();
		}
		catch(Exception er){
			er.printStackTrace();
		}
	}

	/**
	 * This method initializes jButtonStampa	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonStampa() {
		if (jButtonStampa == null) {
			jButtonStampa = new JButton();
			jButtonStampa.setBounds(new Rectangle(338, 20, 135, 30));
			jButtonStampa.setText("Stampa Ingresso");
			jButtonStampa.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					jFrame.setEnabled(false);
					stampaIn();
					jFrame.setEnabled(true);
				}
			});
		}
		return jButtonStampa;
	}
	
	private void stampaIn(){
		try{
			if(idassext == -1 || idcliente == -1)
				JOptionPane.showMessageDialog(jFrame, "Selezionare l'assistenza da stampare.", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			else{
				Map par = new HashMap();
				par.put("cf", idcliente);
				par.put("id", idassext);
				JasperPrint jasperPrint = JasperFillManager.fillReport("AssExt3.jasper", par, dbm.getConnessione());
				JasperViewer.viewReport(jasperPrint, false); 
				//JasperPrintManager.printReport(JasperFillManager.fillReport("AssExt3.jasper", par, this.dbm.getConnessione()), true);
			}
		}
		catch(Exception er){
			er.printStackTrace();
		}
	}

	/**
	 * This method initializes jPanelBottoni	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelBottoni() {
		if (jPanelBottoni == null) {
			jPanelBottoni = new JPanel();
			jPanelBottoni.setLayout(null);
			jPanelBottoni.setSize(400, 200);
			jPanelBottoni.setPreferredSize(new Dimension(250, 120));
			jPanelBottoni.add(getJButtonInserisciAss(), null);
			jPanelBottoni.add(getJButtonAggiorna(), null);
			jPanelBottoni.add(getJButtonCancellaAss(), null);
			jPanelBottoni.add(getJButtonCercaAss(), null);
			jPanelBottoni.add(getJButtonClose(), null);
			jPanelBottoni.add(getJButtonStampa(), null);
			jLabelInfoRicerca = new JLabel();
			jLabelInfoRicerca.setText("La ricerca viene effettuata tramite il campo ID.");
			jLabelInfoRicerca.setBounds(new Rectangle(20, 20, 300, 16));
			jPanelBottoni.add(jLabelInfoRicerca, null);
			jPanelBottoni.add(getJTextFieldCercaID(), null);
			jPanelBottoni.add(getJButtonStampaOut(), null);
		}
		return jPanelBottoni;
	}

	/**
	 * This method initializes jPanelVista	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelVista() {
		if (jPanelVista == null) {
			jPanelVista = new JPanel();
			jPanelVista.setLayout(null);
			
			jPanelVista.setName("pnlVista");
			jPanelVista.setPreferredSize(new Dimension(740, 50));
			/*jPanelVista.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory
					.createBevelBorder(javax.swing.border.BevelBorder.RAISED), "Dati assistenza",
					javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
					javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null)); // Generated*/
			jPanelVista.add(getJScrollPane(), null);
			jPanelVista.add(getJButtonFiltro(), null);
			jPanelVista.add(getJButtonNuova(), null);
			jPanelVista.add(getJButtonStampaCliente(), null);
		}
		return jPanelVista;
	}
	
	private JXTable getVista() {
		if (vista == null) {
			vista  = new JXTable();
			model = new TableModelAssistenza(dbm, "", "", "assext", "");
			vista .setModel(model);
			vista.setName("vista");
			vista.addMouseListener(new MyMouseAdapter());
			vista .setRowSelectionAllowed(true);
			vista.setCellSelectionEnabled(false);
			vista .setColumnControlVisible(true);
		}
		return vista;
	}
	private JScrollPane		jScrollPane		= null;
	private JTextField jTextFieldCercaID = null;
	private JButton jButtonFiltro = null;
	private JButton jButtonNuova = null;
	private JLabel jLabelCampiObbl = null;
	private JDateChooser jDateChooserDataScon = null;
	private JSpinField jSpinFieldNumScon = null;
	private JDateChooser jDateChooserDataCon = null;
	private JComboBox jComboBoxRiparato = null;
	private JButton jButtonStampaOut = null;
	private JLabel jLabelCortesia = null;
	private JTextField jTextFieldCortesia = null;
	private JLabel jLabelInterventi = null;
	private JTextField jTextFieldIntervento = null;
	private JLabel jLabelRicambi = null;
	private JTextField jTextFieldRicambi = null;
	private JButton jButtonStampaCliente = null;
	private JScrollPane getJScrollPane()
	{
		if (jScrollPane == null)
		{
			try
			{
				jScrollPane = new JScrollPane();
				jScrollPane.setBounds(new Rectangle(10, 10, 720, 215));
				jScrollPane.setViewportView(getVista()); // Generated
			} catch (java.lang.Throwable e){
				e.printStackTrace();
			}
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jPanelCont	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelCont() {
		if (jPanelCont == null) {
			try
			{
				jPanelCont = new JPanel();
				jPanelCont.setLayout(new CardLayout()); // Generated
				jPanelCont.add(getJPanelVista(), getJPanelVista().getName()); // Generated
				jPanelCont.add(getJPanelDati(), getJPanelDati().getName()); // Generated
			} catch (java.lang.Throwable e){
				e.printStackTrace();
			}
			/*jPanelCont = new JPanel();
			jPanelCont.setLayout(new CardLayout());
			jPanelCont.add(getJPanelDati(), java.awt.BorderLayout.WEST); // Generated
			jPanelCont.add(getJPanelVista(), java.awt.BorderLayout.CENTER); // Generated*/
		}
		return jPanelCont;
	}
	
	private void modificaDati(int id){
		try{
			String query = "SELECT * FROM assext WHERE id="+id;
			rs1 = dbm.executeQuery(query);
			if(rs1.next()){
				idassext = rs1.getInt("id");
				jTextFieldImei.setText(rs1.getString("imei"));
				jTextFieldModello.setText(rs1.getString("modello"));
				jTextFieldDifetto.setText(rs1.getString("difetto"));
				if(!rs1.getString("datasc").equals("")){
					StringTokenizer st = new StringTokenizer(rs1.getString("datasc"), "-");
					int a1 = Integer.parseInt(st.nextToken())-1900;
					System.out.println(a1);
					int m1 = Integer.parseInt(st.nextToken())-1;
					System.out.println(m1);
					int g1 = Integer.parseInt(st.nextToken());
					System.out.println(g1);
					jDateChooserDataScon.setDate(new Date(a1,m1,g1));
				}
				else jDateChooserDataScon.setDate(null);
				//jDateChooserDataScon.setDate(new Date(rs1.getString("datasc")));
				jSpinFieldNumScon.setValue(Integer.parseInt(rs1.getString("numsc")));
				if(!rs1.getString("datacon").equals("")){
					StringTokenizer st = new StringTokenizer(rs1.getString("datacon"), "-");
					int a = Integer.parseInt(st.nextToken())-1900;
					System.out.println(a);
					int m = Integer.parseInt(st.nextToken())-1;
					System.out.println(m);
					int g = Integer.parseInt(st.nextToken());
					System.out.println(g);
					jDateChooserDataCon.setDate(new Date(a,m,g));
				}
				else jDateChooserDataCon.setDate(null);
				//jDateChooserDataCon.setDate(new Date(rs1.getString("datacon")));
				jComboBoxRiparato.setSelectedItem(rs1.getString("riparato"));
				jTextFieldCortesia.setText(rs1.getString("cortesia"));
				jTextFieldIntervento.setText(rs1.getString("intervento"));
				jTextFieldAccessori.setText(rs1.getString("accessori"));
				jTextFieldRicambi.setText(rs1.getString("ricambi"));
				idcliente = Integer.parseInt(rs1.getString("cliente"));
				cercaClienteXID();
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}

	/**
	 * This method initializes jTextFieldCercaID	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldCercaID() {
		if (jTextFieldCercaID == null) {
			jTextFieldCercaID = new JTextField();
			jTextFieldCercaID.setBounds(new Rectangle(20, 45, 120, 22));
		}
		return jTextFieldCercaID;
	}

	/**
	 * This method initializes jButtonFiltro	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonFiltro() {
		if (jButtonFiltro == null) {
			jButtonFiltro = new JButton();
			jButtonFiltro.setBounds(new Rectangle(593, 240, 90, 30));
			jButtonFiltro.setText("Filtro");
			jButtonFiltro.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					applicaFiltro();
				}
			});
		}
		return jButtonFiltro;
	}
	
	private void applicaFiltro(){
		try{
			if(jTextFieldCF.getText().trim().equals(""))
				model = new TableModelAssistenza(dbm, "riparato", "In Assistenza", "assext", "");
			else{
				String cliente = String.valueOf(idcliente);
				model = new TableModelAssistenza(dbm, "riparato", "In Assistenza", "assext", cliente);
			}
			vista.setModel(model);
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}

	/**
	 * This method initializes jButtonNuova	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonNuova() {
		if (jButtonNuova == null) {
			jButtonNuova = new JButton();
			jButtonNuova.setBounds(new Rectangle(488, 240, 90, 30));
			jButtonNuova.setText("Nuova");
			jButtonNuova.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					((CardLayout) jPanelCont.getLayout()).show(jPanelCont, "pnlDati");
					//resetCliente();
				}
			});
		}
		return jButtonNuova;
	}

	/**
	 * This method initializes jDateChooserDataScon	
	 * 	
	 * @return com.toedter.calendar.JDateChooser	
	 */
	private JDateChooser getJDateChooserDataScon() {
		if (jDateChooserDataScon == null) {
			jDateChooserDataScon = new JDateChooser();
			jDateChooserDataScon.setBounds(new Rectangle(123, 67, 200, 22));
			jDateChooserDataScon.setDateFormatString("dd-MM-yyyy");
		}
		return jDateChooserDataScon;
	}

	/**
	 * This method initializes jSpinFieldNumScon	
	 * 	
	 * @return com.toedter.components.JSpinField	
	 */
	private JSpinField getJSpinFieldNumScon() {
		if (jSpinFieldNumScon == null) {
			jSpinFieldNumScon = new JSpinField();
			jSpinFieldNumScon.setBounds(new Rectangle(466, 67, 180, 22));
		}
		return jSpinFieldNumScon;
	}

	/**
	 * This method initializes jDateChooserDataCon	
	 * 	
	 * @return com.toedter.calendar.JDateChooser	
	 */
	private JDateChooser getJDateChooserDataCon() {
		if (jDateChooserDataCon == null) {
			jDateChooserDataCon = new JDateChooser();
			jDateChooserDataCon.setBounds(new Rectangle(123, 89, 200, 22));
			jDateChooserDataCon.setDateFormatString("dd-MM-yyyy");
		}
		return jDateChooserDataCon;
	}

	/**
	 * This method initializes jComboBoxRiparato	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBoxRiparato() {
		if (jComboBoxRiparato == null) {
			Vector<String> scelta = new Vector<String>();
			scelta.add("In Assistenza");
			scelta.add("SI");
			scelta.add("NO");
			jComboBoxRiparato = new JComboBox(scelta);
			jComboBoxRiparato.setBounds(new Rectangle(446, 89, 200, 22));
		}
		return jComboBoxRiparato;
	}

	/**
	 * This method initializes jButtonStampaOut	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonStampaOut() {
		if (jButtonStampaOut == null) {
			jButtonStampaOut = new JButton();
			jButtonStampaOut.setBounds(new Rectangle(338, 65, 135, 30));
			jButtonStampaOut.setText("Stampa Uscita");
			jButtonStampaOut.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					jFrame.setEnabled(false);
					stampaOut();
					jFrame.setEnabled(true);
				}
			});
		}
		return jButtonStampaOut;
	}
	
	private void stampaOut(){
		try{
			if(idassext == -1)
				JOptionPane.showMessageDialog(jFrame, "Selezionare l'assistenza da stampare.", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			else{
				Map par = new HashMap();
				par.put("id", idassext);
				JasperPrint jasperPrint = JasperFillManager.fillReport("UscitaExt.jasper", par, dbm.getConnessione());
				JasperViewer.viewReport(jasperPrint, false); 
				//JasperPrintManager.printReport(JasperFillManager.fillReport("UscitaExt.jasper", par, this.dbm.getConnessione()), true);
			}
		}
		catch(Exception er){
			er.printStackTrace();
		}
	}

	/**
	 * This method initializes jTextFieldCortesia	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldCortesia() {
		if (jTextFieldCortesia == null) {
			jTextFieldCortesia = new JTextField();
			jTextFieldCortesia.setBounds(new Rectangle(123, 133, 200, 22));
		}
		return jTextFieldCortesia;
	}

	/**
	 * This method initializes jTextFieldIntervento	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldIntervento() {
		if (jTextFieldIntervento == null) {
			jTextFieldIntervento = new JTextField();
			jTextFieldIntervento.setBounds(new Rectangle(130, 155, 516, 22));
		}
		return jTextFieldIntervento;
	}

	/**
	 * This method initializes jTextFieldRicambi	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldRicambi() {
		if (jTextFieldRicambi == null) {
			jTextFieldRicambi = new JTextField();
			jTextFieldRicambi.setBounds(new Rectangle(446, 133, 200, 22));
		}
		return jTextFieldRicambi;
	}

	/**
	 * This method initializes jButtonStampaCliente	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonStampaCliente() {
		if (jButtonStampaCliente == null) {
			jButtonStampaCliente = new JButton();
			jButtonStampaCliente.setBounds(new Rectangle(338, 240, 135, 30));
			jButtonStampaCliente.setText("Stampa x Cliente");
			jButtonStampaCliente.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent e) {
					jFrame.setEnabled(false);
					stampaCliente();
					jFrame.setEnabled(true);
				}
			});
		}
		return jButtonStampaCliente;
	}
	
	private void stampaCliente(){
		try{
			if(idcliente == -1)
				JOptionPane.showMessageDialog(jFrame, "Selezionare un cliente.", "AVVISO", JOptionPane.INFORMATION_MESSAGE);
			else{
				String assistenza = "'In Assistenza'";
				String cliente = jTextFieldCognome.getText()+"  "+jTextFieldNome.getText();
				Map par = new HashMap();
				par.put("idcliente", idcliente);
				par.put("ass", assistenza);
				par.put("cliente", cliente);
				JasperPrint jasperPrint = JasperFillManager.fillReport("stampaxclientext.jasper", par, dbm.getConnessione());
				JasperViewer.viewReport(jasperPrint, false); 
			}
		} 
		catch (JRException e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		}			
	}
}
